package com.wzutils;

import android.util.Log;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Hu on 2017/3/17.
 */

public class TimeUtils {
    public static String YEAR_MONTH_DAY_HOUR_MIN_SS = "yyyy-MM-dd HH:mm:ss";
    public static String YEAR_MONTH_DAY_HOUR_MIN = "yyyy-MM-dd HH:mm";
    public static String YEAR_MONTH_DAY = "yyyy-MM-dd";
    public static String HOUR_MIN = "HH:mm";

    /**
     * String 转date类型
     *
     * @param str
     * @param dateType
     * @return
     */
    public static Date stringToDate(String str, String dateType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        Date date = null;
        try {
            date = (Date) sdf.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    public static String stringFormat(){

        return null;
    }

    /**
     * Date 格式化成string
     *
     * @param date
     * @param dateType
     * @return
     */
    public static String dateToString(Date date, String dateType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        String dateString = sdf.format(date);
        return dateString;
    }

    /**
     * 将long类型转化为Date
     *
     * @param str
     * @return
     * @throws ParseException
     */
    public static Date longToDate(long str) throws ParseException {
        return new Date(str * 1000);
    }

    /**
     * @param time
     * @param dateType
     * @return
     */
    public static String longToString(long time, String dateType) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateType);
        Date date = new Date(time);
        String str = sdf.format(date);
        return str;
    }

    public static long systemTimeLong() {
        return System.currentTimeMillis();
    }

    public static long stringTolong(String str, String formatType) {
        Date date = stringToDate(str, formatType); // String类型转成date类型
        if (date == null) {
            return 0;
        } else {
            long currentTime = dateToLong(date); // date类型转成long类型
            return currentTime;
        }
    }

    // date类型转换为long类型
    // date要转换的date类型的时间
    public static long dateToLong(Date date) {
        return date.getTime();
    }


    public static long getWebsiteDatetime(String webUrl) {
        try {
            URL url = new URL(webUrl);// 取得资源对象
            URLConnection uc = url.openConnection();// 生成连接对象
            uc.connect();// 发出连接
            long ld = uc.getDate();// 读取网站日期时间
            return ld;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String[] getNowMonthFirstLastday(Calendar calendar, long today) throws java.text.ParseException {
        //获取当前月最后一天
        String[] first_end = new String[2];
        calendar.setTimeInMillis(today);
        first_end[0] = getFirstDayOfMonth(calendar.get(calendar.YEAR), (calendar.get(calendar.MONTH) + 1));
        first_end[1] = getLastDayOfMonth(calendar.get(calendar.YEAR), (calendar.get(calendar.MONTH) + 1));
        L.e(first_end[0] + "--" + first_end[1]);
        return first_end;
    }

    public static String[] getLastMonthFirstLastday(Calendar calendar, long today) throws java.text.ParseException {
        //获取当前月最后一天
        String[] first_end = new String[2];
        calendar.setTimeInMillis(today);
        first_end[0] = getFirstDayOfMonth(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH));
        first_end[1] = getLastDayOfMonth(calendar.get(calendar.YEAR), calendar.get(calendar.MONTH));
        L.e(first_end[0] + "--" + first_end[1]);
        return first_end;
    }

    public static String[] getYearFirstLastDay(Calendar calendar, long today) {
        String[] first_end = new String[2];
        calendar.setTimeInMillis(today);
        first_end[0] = calendar.get(Calendar.YEAR) + "-01-01";
        first_end[1] = calendar.get(Calendar.YEAR) + "-12-31";
        return first_end;
    }

    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2前");
                return -1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2后");
                return 1;
            } else if (dt1.getTime() == dt2.getTime()) {
                return 0;
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return -2;
    }

    /**
     * 得到某年某月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {

        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.YEAR, year);

        cal.set(Calendar.MONTH, month - 1);

        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));


        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }

    /**
     * 得到某年某月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {

        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.YEAR, year);

        cal.set(Calendar.MONTH, month - 1);

        cal.set(Calendar.DAY_OF_MONTH, 1);
        int value = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, value);

        return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());

    }

    public static boolean isOneMonth(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        java.util.Date date1 = null;
        java.util.Date date2 = null;
        try {
            date1 = sdf.parse(smdate);
            date2 = sdf.parse(bdate);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        return date1.getMonth() == date2.getMonth();
    }

    public static boolean isOneYear(String smdate, String bdate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        java.util.Date date1 = null;
        java.util.Date date2 = null;
        try {
            date1 = sdf.parse(smdate);
            date2 = sdf.parse(bdate);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        return date1.getYear() == date2.getYear();
    }

    public static String getMonth_Day(Calendar calendar, String dateTime, String formatType) throws java.text.ParseException {
        Date date = stringToDate(dateTime, formatType);
        calendar.setTime(date);
        String first = ((calendar.get(Calendar.MONTH) + 1) > 9 ? "" : "0") + (calendar.get(Calendar.MONTH) + 1) + "."
                + ((calendar.get(Calendar.DAY_OF_MONTH) + 1) > 9 ? "" : "0") + calendar.get(Calendar.DAY_OF_MONTH);
        return first;
    }

    public static String getMonth(Calendar calendar, String dateTime, String formatType) throws java.text.ParseException {
        Date date = stringToDate(dateTime, formatType);
        calendar.setTime(date);
        String first = ((calendar.get(Calendar.MONTH) + 1) > 9 ? "" : "0") + (calendar.get(Calendar.MONTH) + 1) + "月";
        return first;
    }

    public static String getYear(Calendar calendar, String dateTime, String formatType) throws java.text.ParseException {
        Date date = stringToDate(dateTime, formatType);
        calendar.setTime(date);
        String first = calendar.get(Calendar.YEAR) + "年";
        return first;
    }

    public static String[] getDayAndTime(Calendar calendar, String dateTime) {
        String[] day_time = new String[2];
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date date = null;
        try {
            date = dateFormat.parse(dateTime);
        } catch (android.net.ParseException e) {
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        String day = calendar.get(Calendar.YEAR) + "-" + (
                (calendar.get(Calendar.MONTH) + 1) > 9 ? (calendar.get(Calendar.MONTH) + 1)
                        : ("0" + (calendar.get(Calendar.MONTH) + 1))) + "-" + (
                calendar.get(Calendar.DAY_OF_MONTH) > 9 ? calendar.get(Calendar.DAY_OF_MONTH) :
                        ("0" + calendar.get(Calendar.DAY_OF_MONTH)));
        String time = (calendar.get(Calendar.HOUR) > 9 ? calendar.get(Calendar.HOUR) : ("0" + calendar.get(Calendar.HOUR))) +
                ":" + (calendar.get(Calendar.MINUTE) > 9 ? calendar.get(Calendar.MINUTE) : ("0" + calendar.get(Calendar.MINUTE)));

        day_time[0] = day;
        day_time[1] = time;
        return day_time;
    }


    public static String getYesterday(String timeType) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        String yesterday = new SimpleDateFormat(timeType).format(cal.getTime());
        return yesterday;

    }
}
